//以下为修改jQuery Validation插件兼容Bootstrap的方法，没有直接写在插件中是为了便于插件升级
$.validator.setDefaults({
	highlight: function (element) {
		$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    },success: function (element) {
    	element.closest('.form-group').removeClass('has-error').addClass('has-success');
    },errorElement: "span",
    errorPlacement: function (error, element) {
        if (element.is(":radio") || element.is(":checkbox")) {
            error.appendTo(element.parent().parent().parent());
        } else {
            error.appendTo(element.parent());
        }
    },errorClass: "help-block m-b-none",
    validClass: "help-block m-b-none"
});

$().ready(function () {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#loginForm").validate({
        rules: {
            name: "required",
            password: "required"
        },
        messages: {
        	name: icon + "用户名不能为空",
        	password: icon + "密码不能为空"
        }
    });
	
	$("#loginBtn").on("click",function(){
		if($("#loginForm").validate().form()){
			$.ajax({
				url :"../admin/login",
				type : "post",
				dataType:"json",
				data : $('#loginForm').serialize(),
				success : function(data){
					console.info(data);
					if(data.success){
						$("#msg").addClass("hidden");
						window.location.href="../admin/indexpage";
					}else{
						$("#msg").html(data.msg).removeClass("hidden");
					}
				},error : function(a,b,c){
					$("#msg").html("系统异常！").removeClass("hidden");
				}
			});
		}
	});
});